/*	TGDefaultsManager.m
 *	Part of ThinkGeek LED Clock
 *	http://tmorgan.shutupgeorge.com/thinkgeek/
 *
 * Copyright (c)2003-2006 Tim Morgan. All rights reserved. Please visit
 * http://tmorgan.shutupgeorge.com/other/license.html for more information about
 * your rights and responsibilities regarding this source code.
 */

#import "TGDefaultsManager.h"

#import "TGThemeManager.h"

const NSString *TGLatestPreferencesVersion = @"1";

@implementation TGDefaultsManager

- (id) init {
	if (self = [super init]) {
		defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"org.tmorgan.ThinkGeekLEDClock"];
		NSDictionary *standardDefaults = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"StandardDefaults" ofType:@"plist"]];
		[defaults registerDefaults:standardDefaults];
	}
	return self;
}

- (void) awakeFromNib {
	NSString *versionString = [defaults stringForKey:@"version"];
	if (![versionString isEqualToString:(NSString *)TGLatestPreferencesVersion]) {
		// ohp, new defaults version! time to reset defaults
		NSDictionary *standardDefaults = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"StandardDefaults" ofType:@"plist"]];
		NSEnumerator *keyEnum = [standardDefaults keyEnumerator];
		id curKey;
		while (curKey = [keyEnum nextObject])
			[defaults setObject:[standardDefaults objectForKey:curKey] forKey:curKey];
	}
}

- (NSColor *) backgroundColor {
	return [defaults objectForKey:@"backgroundColor"];
}

- (void) setBackgroundColor:(NSColor *)newColor {
	[defaults setObject:newColor forKey:@"backgroundColor"];
	[defaults synchronize];
}

- (NSString *) backgroundImageFilePath {
	return [defaults stringForKey:@"backgroundImageFilePath"];
}

- (void) setBackgroundImageFilePath:(NSString *)newPath {
	[defaults setObject:newPath forKey:@"backgroundImageFilePath"];
	[defaults synchronize];
}

- (BOOL) validateBackgroundImageFilePath:(NSString **)path error:(NSError **)outError {
	if (*path == nil) return YES;
	NSError *error = nil;
	NSData *testData = [[NSData alloc] initWithContentsOfFile:*path options:NSUncachedRead error:&error];
	if (testData) [testData release];
	if (error == nil) return YES;
	else {
		*outError = [error autorelease];
		return NO;
	}
}

- (NSString *) backgroundStyle {
	return [defaults stringForKey:@"backgroundStyle"];
}

- (void) setBackgroundStyle:(NSString *)newStyle {
	[defaults setObject:newStyle forKey:@"backgroundStyle"];
	[defaults synchronize];
}

- (BOOL) validateBackgroundStyle:(NSString **)style error:(NSError **)outError {
	if ([[TGThemeManager themeManager] isValidBackgroundStyle:*style]) return YES;
	*outError = [[TGErrorManager errorManager] errorOfType:TGErrorInvalidBackgroundStyle];
	return NO;
}

- (BOOL) checkUpdatesAutomatically {
	return [defaults boolForKey:@"checkUpdatesAutomatically"];
}

- (void) setCheckUpdatesAutomatically:(BOOL)willCheck {
	[defaults setBool:willCheck forKey:@"checkUpdatesAutomatically"];
	[defaults synchronize];
}

- (int) clockSize {
	return [defaults integerForKey:@"clockSize"];
}

- (void) setClockSize:(int)newSize {
	[defaults setInteger:newSize forKey:@"clockSize"];
	[defaults synchronize];
}

- (BOOL) validateClockSize:(int *)size error:(NSError **)outError {
	if (*size > 10) *size = 10;
	if (*size < 2) *size = 2;
	return YES;
}

- (NSString *) clockTheme {
	return [defaults stringForKey:@"clockTheme"];
}

- (BOOL) validateClockTheme:(NSString **)theme error:(NSError **)outError {
	if ([[TGThemeManager themeManager] isValidClockTheme:*theme]) return YES;
	*outError = [[TGErrorManager errorManager] errorOfType:TGErrorInvalidClockTheme];
	return NO;
}

- (void) setClockTheme:(NSString *)newTheme {
	[defaults setObject:newTheme forKey:@"clockTheme"];
	[defaults synchronize];
}

- (BOOL) timeFormat24Hour {
	return [defaults boolForKey:@"timeFormat24Hour"];
}

- (void) setTimeFormat24Hour:(BOOL)is24Hr {
	[defaults setBool:is24Hr forKey:@"timeFormat24Hour"];
	[defaults synchronize];
}

@end
